/*
 * Copyright@www.it1997.com.
 * @Author: 陈晓晨
 * @Date: 2020/7/1
 * Description:版权所有 QQ:376231445
 */

package com.it1997.it1997admin.service.impl;

import com.it1997.it1997admin.mapper.ExtractDataMapper;
import com.it1997.it1997admin.pojo.KsData;
import com.it1997.it1997admin.pojo.OtherKmKsNumber;
import com.it1997.it1997admin.service.ExtractDataService;
import com.it1997.it1997admin.utils.CreatePdf;
import com.it1997.it1997admin.utils.XkCreatePdf;
import com.itextpdf.text.*;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfWriter;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.File;
import java.io.FileOutputStream;
import java.util.List;

@Log4j2
@Service
public class ExtractDataServiceImpl implements ExtractDataService {
    @Autowired
    private ExtractDataMapper tractDataMapper;
    @Autowired
    private ExtractDataService extractDataService;
    @Override
    public List<KsData> getAllKd(Integer mode, String KDDM, Integer allData, Integer rate) {
        Integer extractData = (int) (allData * rate / 100);
        return tractDataMapper.getAllKd(mode, KDDM, extractData);
    }
    @Override
    public List<KsData> getRandomKs(Integer KCSL,String KDDM, Integer extractKSData) {

        return tractDataMapper.getRandomKs(KCSL,KDDM, extractKSData);
    }
    @Override
    public List<KsData> getRandomFkKs(String KSH) {
        return tractDataMapper.getRandomFKKs(KSH);
    }

    @Override
    public Integer getKDKCSL(String KDDM) {
        return tractDataMapper.getKDKCSL(KDDM);
    }

    @Override
    public int extractData(Integer mode, String KDDM, Integer extractKSNumber, Integer rate) {
        Integer allKDNumber = 393; //所有考点数量
        //抽取一定比例的考点信息
        List<KsData> extractKd = extractDataService.getAllKd(mode, KDDM, allKDNumber, rate);
        log.info(extractKd);
        for (KsData ksData : extractKd) {
            //查询该考点的考场数
            Integer KCSL = extractDataService.getKDKCSL(ksData.getKDDM());
            try {
                //每个考点随机抽取？位考生
                List<KsData> ksDataList = extractDataService.getRandomKs(KCSL,ksData.getKDDM(), (int) (extractKSNumber));
                if (ksData.getKDDM() == null) {
                    return 0;
                }

                String dataDir = "D:/data/抽取数据/";
                File file = new File(dataDir);
                if (!file.exists()) {
                    file.mkdir();
                }
                String NewPath = dataDir + "考点" + ksData.getKDDM() + ".pdf";
                CreatePdf createPdf = new CreatePdf(extractDataService,ksDataList,NewPath,ksData.getKDDM(),KCSL,ksData.getKSSL());
                XkCreatePdf xkCreatePdf= new XkCreatePdf(extractDataService,ksDataList,NewPath,ksData.getKDDM(),KCSL,ksData.getKSSL());
                new Thread(xkCreatePdf).start();
            } catch (Exception e) {
                log.error(e.getMessage());
            }
        }
        return 1;
    }

    @Override
    public List<OtherKmKsNumber> getOtherKmKsNumber(String kddm) {
        return tractDataMapper.tractDataMapper(kddm);
    }
}
